// components/login-alert/login-alert.js
let app = getApp();
import Dialog from '../../miniprogram/miniprogram_npm/vant-weapp/dialog/dialog'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isShow: {
      type: Boolean,
      default: false,
      observer(newVal,oldVal,changedPath){
        this.setData({
          showCom: newVal
        })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showCom: false,
    showNumber:false,
    sessionKey:'',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //登录，获取session_key
    wxLogin() {
      let _this = this;
      wx.login({
        success: function (res) {
          var code = res.code;
          if (res.code) {
            wx.getUserInfo({
              success: function (res) {
                app.requestUrl({
                  method: 'POST',
                  url: '/wxapi/app/login',
                  data: {
                    code: code,
                    centreId: app.globalData.loginCentreId,
                    flag: 1,
                  },
                  load: 0,
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                }).then((data)=>{
                  if (data.data.success)
                    _this.wxUserInfo(res, data.data.data);
                });
              }
            })
          }
        }
      })
    },

    //得到用户信息
    wxUserInfo(res, encryptInfo) {
      let that = this;
      app.requestUrl({
        method: 'GET',
        url: '/wxapi/app/sendXcxUnionId',
        data: {
          encryptedData: res.encryptedData,
          iv: res.iv,
          sessionKey: encryptInfo,
          teamid:app.getShop().id,
          mid: app.globalData.tempMid,//推荐人id
          refOpenId:wx.getStorageSync('shareOpenid')//直播分享者id
        },
        load: 0,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
      }).then((data)=>{
        //4.解密成功后 获取自己服务器返回的结果
        if (data.data.success) {

          let info = data.data.data;
          wx.setStorageSync('userInfo', JSON.stringify(info));//保存用户信息
          wx.setStorageSync('firstLogin', data.data.firstLogin);//false 老用户、true新用户
          wx.setStorageSync('authToken', data.data.authToken);//保存身份验证
          if(info.mobile == ''){
            that.setData({
              showNumber:true,
              sessionKey: encryptInfo,
            });
          }else{
              // 获取当前的页面栈
              let pages = getCurrentPages();
              if(pages[0].route == 'pages/index/index'){
                pages[0].onShow();
              } else {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
          };

        }
      })
    },
    getPhoneNumber(e){
      let that = this;
      if (e.detail.errMsg == "getPhoneNumber:ok") {
        app.requestUrl({
          method: 'POST',
          url: '/wxapi/getEncryptedUserInfo',
          data: {
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            sessionKey: that.data.sessionKey,
          },
          load: 0,
        }).then((data)=>{
          if (data.data.success){
            // 获取当前的页面栈
            let pages = getCurrentPages();
            if(pages[0].route == 'pages/index/index'){
              pages[0].onShow();
            } else {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          }
            
        });
      }
      
    },
    onClose(){
      this.triggerEvent('acceptIsShow', false)
    },
  }
})
